package com.swpu.service_weight.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.swpu.service_weight.entity.WeightRecord;
import com.swpu.service_weight.entity.vo.MonthDataVo;
import com.swpu.service_weight.entity.vo.WeightQueryVo;
import com.swpu.service_weight.entity.vo.WeightRecordVo;
import com.swpu.service_weight.mapper.WeightRecordMapper;
import com.swpu.service_weight.service.WeightRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.joda.time.LocalDateTime;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chaichai
 * @since 2024-02-01
 */
@Service
public class WeightRecordServiceImpl extends ServiceImpl<WeightRecordMapper, WeightRecord> implements WeightRecordService {

    //保存用户每日体重数据
    @Override
    public void recordUserWeight(String openid, WeightRecordVo weightVo) {
        WeightRecord weightRecord = new WeightRecord();
        weightRecord.setWeight(weightVo.getWeight());
        weightRecord.setUserId(openid);
        weightRecord.setRecordTime(weightVo.getRecordTime());
        boolean isSave = this.save(weightRecord);
        if (!isSave) throw new RuntimeException("用户体重数据保存失败");
    }

    @Override
    public List<MonthDataVo> getUserWeightMonthData(String openid, WeightQueryVo weightQueryVo) {
        //字符串转Date类型
        Date queryDate=null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            queryDate = format.parse(weightQueryVo.getCurrentMonthDay());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        DateTime beginOfMonth = DateUtil.beginOfMonth(queryDate);
        DateTime endOfMonth = DateUtil.endOfMonth(queryDate);
//        LambdaQueryWrapper<WeightRecord> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(WeightRecord::getUserId,openid);
//        queryWrapper.between(WeightRecord::getRecordTime,beginOfMonth,endOfMonth);
//        queryWrapper.orderByDesc(WeightRecord::getCreateTime);
//        List<WeightRecord> recordList = this.list(queryWrapper);
        List<MonthDataVo> monthDayAvgDataList = baseMapper.getMonthDayAvgData(openid,beginOfMonth, endOfMonth );
        return monthDayAvgDataList;
    }

    @Override
    public List<WeightRecord> getWeightMonthAllData(String openid, WeightQueryVo weightQueryVo) {
        Date queryDate=null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            queryDate = format.parse(weightQueryVo.getCurrentMonthDay());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        DateTime beginOfMonth = DateUtil.beginOfMonth(queryDate);
        DateTime endOfMonth = DateUtil.endOfMonth(queryDate);
        LambdaQueryWrapper<WeightRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WeightRecord::getUserId,openid)
                .between(WeightRecord::getRecordTime,beginOfMonth,endOfMonth);
        List<WeightRecord> weightRecordList = this.list(queryWrapper);
        return weightRecordList;
    }

    @Override
    public List<WeightRecord> getWeightDataToChart(String openid, int page, int pageSize) {
        Page<WeightRecord> recordPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<WeightRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WeightRecord::getUserId,openid).orderByDesc(WeightRecord::getRecordTime);
        this.page(recordPage,queryWrapper);
        List<WeightRecord> records = recordPage.getRecords();
        return records;
    }

    @Override
    public Double getLatestWeightData(String openid) {
        LambdaQueryWrapper<WeightRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(WeightRecord::getRecordTime);
        WeightRecord latestRecord = this.getOne(queryWrapper);
        Double latestWeight = latestRecord.getWeight();
        return latestWeight;
    }
}
